package com.example.PolimiAssistant;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.Wsdl2Code.WebServices.PolimiAssistant.CourseInfo;
import com.Wsdl2Code.WebServices.PolimiAssistant.PolimiAssistant;
import com.example.PolimiAssistant.common.CommonActivity;
import com.example.PolimiAssistant.common.ExamScheduleAdapter;
import com.example.PolimiAssistant.utils.PreferencesManager;

import java.util.Vector;

/**
 * ExamsSchedule.java
 *
 * This activity display to the user, the information about the upcoming
 * exams that he/she can take. Starting reg date, ending reg date, exam
 * date and if the user is registered on them.
 *
 * Todo: Possibly add an alarm functionality for the starting/ending/presentation date
 */
public class ExamsSchedule extends CommonActivity {

    private Vector<CourseInfo> coursesCollection;
    private ExamScheduleAdapter listAdapter;
    private ListView examscheduleList;

    public void onCreate(Bundle savedInstanceState) {

        //Activity initialization
        super.onCreate(savedInstanceState);
        setContentView(R.layout.exam_schedule);

        examscheduleList = (ListView) findViewById(R.id.examscheduleList);

        // Click event for single list row
        examscheduleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Intent i = new Intent();
                i.setClass(ExamsSchedule.this, ExamSchedule_detail.class);
                CourseInfo c_selected = coursesCollection.get(position);
                i.putExtra("id",c_selected.id);
                i.putExtra("name",c_selected.name);
                i.putExtra("credits",c_selected.credits);
                i.putExtra("major_name",c_selected.major_name);
                i.putExtra("school_name",c_selected.school_name);
                i.putExtra("semestar_name",c_selected.semestar_name);
                startActivity(i);
            }
        });

        //Update the information on the views
        //new UpdateData().execute();
        final Context context = this;
        new AsyncTask<Void, Void, Vector<CourseInfo>>() {
            ProgressDialog loadingDialog;
            {
                loadingDialog = new ProgressDialog(ExamsSchedule.this);
            }
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loadingDialog.setMessage("Loading...");
                loadingDialog.show();
            }
            @Override
            protected Vector<CourseInfo> doInBackground(Void... params) {
                PolimiAssistant polimiService = new PolimiAssistant();
                PreferencesManager p = new PreferencesManager(context);
                String token = p.getToken();
                Vector<CourseInfo> cList = polimiService.GetUserNotPassCourses(token);

                if(cList != null && cList.size() > 0) {
                    CourseInfo status = cList.get(0);
                    if (!status.valid) {
                        //TODO: signal the android that token is expiried to logout the user
                    }
                }
                if(cList == null) {
                    cList = new Vector<CourseInfo>();
                    Log.e(TAG, "LOAD FAILURE: GetUserNotPassCourses RETURN NULL");
                }

                coursesCollection = cList;
                return cList;
            }

            @Override
            protected void onPostExecute(Vector<CourseInfo> list) {
                super.onPostExecute(list);
                listAdapter = new ExamScheduleAdapter(context,R.layout.exam_schedule_list_item, list);
                listAdapter.notifyDataSetChanged();
                examscheduleList.setAdapter(listAdapter);
                loadingDialog.dismiss();
            }

        }.execute();
    }
}